{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 适合初学者快速入门的Numpy实战全集"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "本文由光城同学整理\n",
    "\n",
    "Numpy是一个用python实现的科学计算的扩展程序库，包括：\n",
    "* 1、一个强大的N维数组对象Array；\n",
    "* 2、比较成熟的（广播）函数库；\n",
    "* 3、用于整合C/C++和Fortran代码的工具包；\n",
    "* 4、实用的线性代数、傅里叶变换和随机数生成函数。numpy和稀疏矩阵运算包scipy配合使用更加方便。\n",
    "\n",
    "NumPy（Numeric Python）提供了许多高级的数值编程工具，如：矩阵数据类型、矢量处理，以及精密的运算库。专为进行严格的数字处理而产生。多为很多大型金融公司使用，以及核心的科学计算组织如：Lawrence Livermore，NASA用其处理一些本来使用C++，Fortran或Matlab等所做的任务。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 本文目录\n",
    "### 1.Numpy基本操作\n",
    "#### 1.1 列表转为矩阵\n",
    "#### 1.2 维度\n",
    "#### 1.3 行数和列数()\n",
    "#### 1.4 元素个数\n",
    "### 2.Numpy创建array\n",
    "#### 2.1 一维array创建\n",
    "#### 2.2 多维array创建\n",
    "#### 2.3 创建全零数组\n",
    "#### 2.4 创建全1数据\n",
    "#### 2.5 创建全空数组\n",
    "#### 2.6 创建连续数组\n",
    "#### 2.7 reshape操作\n",
    "#### 2.8 创建连续型数据\n",
    "#### 2.9 linspace的reshape操作\n",
    "### 3.Numpy基本运算\n",
    "#### 3.1 一维矩阵运算\n",
    "#### 3.2 多维矩阵运算\n",
    "#### 3.3 基本计算\n",
    "### 4.Numpy索引与切片\n",
    "### 5.Numpy array合并\n",
    "#### 5.1 数组合并\n",
    "#### 5.2 数组转置为矩阵\n",
    "#### 5.3 多个矩阵合并\n",
    "#### 5.4 合并例子2\n",
    "### 6.Numpy array分割\n",
    "#### 6.1 构造3行4列矩阵\n",
    "#### 6.2 等量分割\n",
    "#### 6.3 不等量分割\n",
    "#### 6.4 其他的分割方式\n",
    "### 7.Numpy copy与 =\n",
    "#### 7.1 =赋值方式会带有关联性\n",
    "#### 7.2 copy()赋值方式没有关联性\n",
    "### 8.广播机制\n",
    "### 9.常用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 1.Numpy基本操作"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.1 列表转为矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 3 5]\n",
      " [4 6 9]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "array = np.array([\n",
    "    [1,3,5],\n",
    "    [4,6,9]\n",
    "])\n",
    "\n",
    "print(array)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.2 维度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "number of dim: 2\n"
     ]
    }
   ],
   "source": [
    "print('number of dim:', array.ndim)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.3 行数和列数()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "shape: (2, 3)\n"
     ]
    }
   ],
   "source": [
    "print('shape:',array.shape)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 1.4 元素个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "size: 6\n"
     ]
    }
   ],
   "source": [
    "print('size:',array.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2.Numpy创建array"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.1 一维array创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2 23  4]\n",
      "int32\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 一维array\n",
    "a = np.array([2,23,4], dtype=np.int32) # np.int默认为int32\n",
    "print(a)\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.2 多维array创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 3 4]\n",
      " [3 4 5]]\n"
     ]
    }
   ],
   "source": [
    "# 多维array\n",
    "a = np.array([[2,3,4],\n",
    "              [3,4,5]])\n",
    "print(a) # 生成2行3列的矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.3 创建全零数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "a = np.zeros((3,4))\n",
    "print(a) # 生成3行4列的全零矩阵"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.4 创建全1数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1 1]\n",
      " [1 1 1 1]\n",
      " [1 1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "# 创建全一数据，同时指定数据类型\n",
    "a = np.ones((3,4),dtype=np.int)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.5 创建全空数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]\n",
      " [0. 0. 0. 0.]]\n"
     ]
    }
   ],
   "source": [
    "# 创建全空数组，其实每个值都是接近于零的数\n",
    "a = np.empty((3,4))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.6 创建连续数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 12 14 16 18 20]\n"
     ]
    }
   ],
   "source": [
    "# 创建连续数组\n",
    "a = np.arange(10,21,2) # 10-20的数据，步长为2\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.7 reshape操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[10 12 14]\n",
      " [16 18 20]]\n"
     ]
    }
   ],
   "source": [
    "# 使用reshape改变上述数据的形状\n",
    "b = a.reshape((2,3))\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.8 创建连续型数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 1.          1.47368421  1.94736842  2.42105263  2.89473684  3.36842105\n",
      "  3.84210526  4.31578947  4.78947368  5.26315789  5.73684211  6.21052632\n",
      "  6.68421053  7.15789474  7.63157895  8.10526316  8.57894737  9.05263158\n",
      "  9.52631579 10.        ]\n"
     ]
    }
   ],
   "source": [
    "# 创建线段型数据\n",
    "a = np.linspace(1,10,20) # 开始端1，结束端10，且分割成20个数据，生成线段\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 2.9 linspace的reshape操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1.          1.47368421  1.94736842  2.42105263]\n",
      " [ 2.89473684  3.36842105  3.84210526  4.31578947]\n",
      " [ 4.78947368  5.26315789  5.73684211  6.21052632]\n",
      " [ 6.68421053  7.15789474  7.63157895  8.10526316]\n",
      " [ 8.57894737  9.05263158  9.52631579 10.        ]]\n"
     ]
    }
   ],
   "source": [
    "# 同时也可以reshape\n",
    "b = a.reshape((5,4))\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3.Numpy基本运算"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.1 一维矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 20 30 40] [0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# 一维矩阵运算\n",
    "a = np.array([10,20,30,40])\n",
    "b = np.arange(4)\n",
    "print(a,b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[10 19 28 37]\n"
     ]
    }
   ],
   "source": [
    "c = a - b\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[  0  20  60 120]\n"
     ]
    }
   ],
   "source": [
    "print(a*b) # 若用a.dot(b),则为各维之和"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 4 9]\n"
     ]
    }
   ],
   "source": [
    "# 在Numpy中，想要求出矩阵中各个元素的乘方需要依赖双星符号 **，以二次方举例，即：\n",
    "c = b**2\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[-0.54402111  0.91294525 -0.98803162  0.74511316]\n"
     ]
    }
   ],
   "source": [
    "# Numpy中具有很多的数学函数工具\n",
    "c = np.sin(a)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True  True False False]\n"
     ]
    }
   ],
   "source": [
    "print(b<2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[False  True False  True]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([1,1,4,3])\n",
    "b = np.arange(4)\n",
    "print(a==b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.2 多维矩阵运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1]\n",
      " [0 1]]\n"
     ]
    }
   ],
   "source": [
    "a = np.array([[1,1],[0,1]])\n",
    "b = np.arange(4).reshape((2,2))\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "print(b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 多维度矩阵乘法\n",
    "# 第一种乘法方式:\n",
    "c = a.dot(b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4]\n",
      " [2 3]]\n"
     ]
    }
   ],
   "source": [
    "# 第二种乘法:\n",
    "c = np.dot(a,b)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3.825517216750851\n"
     ]
    }
   ],
   "source": [
    "# 多维矩阵乘法不能直接使用'*'号\n",
    "\n",
    "a = np.random.random((2,4))\n",
    "\n",
    "print(np.sum(a)) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.09623355767721398\n"
     ]
    }
   ],
   "source": [
    "print(np.min(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.7420428188342583\n"
     ]
    }
   ],
   "source": [
    "print(np.max(a))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a= [[0.48634962 0.74204282 0.09623356 0.69074812]\n",
      " [0.60218881 0.52734181 0.41434585 0.26626662]]\n"
     ]
    }
   ],
   "source": [
    "print(\"a=\",a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果你需要对行或者列进行查找运算，\n",
    "\n",
    "就需要在上述代码中为 axis 进行赋值。\n",
    "\n",
    "当axis的值为0的时候，将会以列作为查找单元，\n",
    "\n",
    "当axis的值为1的时候，将会以行作为查找单元。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "sum= [2.01537412 1.8101431 ]\n"
     ]
    }
   ],
   "source": [
    "print(\"sum=\",np.sum(a,axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "min= [0.48634962 0.52734181 0.09623356 0.26626662]\n"
     ]
    }
   ],
   "source": [
    "print(\"min=\",np.min(a,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "max= [0.74204282 0.60218881]\n"
     ]
    }
   ],
   "source": [
    "print(\"max=\",np.max(a,axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 3.3 基本计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 2  3  4  5]\n",
      " [ 6  7  8  9]\n",
      " [10 11 12 13]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "A = np.arange(2,14).reshape((3,4))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n"
     ]
    }
   ],
   "source": [
    "# 最小元素索引\n",
    "print(np.argmin(A)) # 0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "11\n"
     ]
    }
   ],
   "source": [
    "# 最大元素索引\n",
    "print(np.argmax(A)) # 11"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "# 求整个矩阵的均值\n",
    "print(np.mean(A)) # 7.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "print(np.average(A)) # 7.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "print(A.mean()) # 7.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.5\n"
     ]
    }
   ],
   "source": [
    "# 中位数\n",
    "print(np.median(A)) # 7.5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 2  5  9 14 20 27 35 44 54 65 77 90]\n"
     ]
    }
   ],
   "source": [
    "# 累加\n",
    "print(np.cumsum(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2 4]\n",
      " [4 2]]\n"
     ]
    }
   ],
   "source": [
    "# 累差运算\n",
    "B = np.array([[3,5,9],\n",
    "              [4,8,10]])\n",
    "print(np.diff(B))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))\n",
      "(array([0, 0, 1, 1], dtype=int64), array([1, 2, 0, 2], dtype=int64))\n"
     ]
    }
   ],
   "source": [
    "C = np.array([[0,5,9],\n",
    "              [4,0,10]])\n",
    "print(np.nonzero(B))\n",
    "print(np.nonzero(C))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14 13 12 11]\n",
      " [10  9  8  7]\n",
      " [ 6  5  4  3]]\n"
     ]
    }
   ],
   "source": [
    "# 仿照列表排序\n",
    "A = np.arange(14,2,-1).reshape((3,4)) # -1表示反向递减一个步长\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[11 12 13 14]\n",
      " [ 7  8  9 10]\n",
      " [ 3  4  5  6]]\n"
     ]
    }
   ],
   "source": [
    "print(np.sort(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14 10  6]\n",
      " [13  9  5]\n",
      " [12  8  4]\n",
      " [11  7  3]]\n"
     ]
    }
   ],
   "source": [
    "# 矩阵转置\n",
    "print(np.transpose(A))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14 10  6]\n",
      " [13  9  5]\n",
      " [12  8  4]\n",
      " [11  7  3]]\n"
     ]
    }
   ],
   "source": [
    "print(A.T)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[14 13 12 11]\n",
      " [10  9  8  7]\n",
      " [ 6  5  4  3]]\n"
     ]
    }
   ],
   "source": [
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[9 9 9 9]\n",
      " [9 9 8 7]\n",
      " [6 5 5 5]]\n"
     ]
    }
   ],
   "source": [
    "print(np.clip(A,5,9))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "clip(Array,Array_min,Array_max)\n",
    "\n",
    "将Array_min<X<Array_max  X表示矩阵A中的数，如果满足上述关系，则原数不变。\n",
    "\n",
    "否则，如果X<Array_min，则将矩阵中X变为Array_min;\n",
    "\n",
    "如果X>Array_max，则将矩阵中X变为Array_max."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4.Numpy索引与切片"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  4  5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(3,15)\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "print(A[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 3  4  5  6]\n",
      " [ 7  8  9 10]\n",
      " [11 12 13 14]]\n"
     ]
    }
   ],
   "source": [
    "B = A.reshape(3,4)\n",
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "print(B[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(B[0][2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "print(B[0,2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[8 9]\n"
     ]
    }
   ],
   "source": [
    "# list切片操作\n",
    "print(B[1,1:3]) # [8 9] 1:3表示1-2不包含3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[3 4 5 6]\n",
      "[ 7  8  9 10]\n",
      "[11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "for row in B:\n",
    "    print(row)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  7 11]\n",
      "[ 4  8 12]\n",
      "[ 5  9 13]\n",
      "[ 6 10 14]\n"
     ]
    }
   ],
   "source": [
    "# 如果要打印列，则进行转置即可\n",
    "for column in B.T:\n",
    "    print(column)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 3  4  5  6  7  8  9 10 11 12 13 14]\n"
     ]
    }
   ],
   "source": [
    "# 多维转一维\n",
    "A = np.arange(3,15).reshape((3,4))\n",
    "# print(A)\n",
    "print(A.flatten())\n",
    "# flat是一个迭代器，本身是一个object属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n",
      "4\n",
      "5\n",
      "6\n",
      "7\n",
      "8\n",
      "9\n",
      "10\n",
      "11\n",
      "12\n",
      "13\n",
      "14\n"
     ]
    }
   ],
   "source": [
    "for item in A.flat:\n",
    "    print(item)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们一起来来总结一下，看下面切片取值方式（对应颜色是取出来的结果）：\n",
    "\n",
    "![](images/1.png)\n",
    "\n",
    "![](images/2.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5.Numpy array合并"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.1 数组合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1]\n",
      " [2 2 2]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.array([1,1,1])\n",
    "B = np.array([2,2,2])\n",
    "print(np.vstack((A,B)))\n",
    "# vertical stack 上下合并,对括号的两个整体操作。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1]\n",
      " [2 2 2]]\n"
     ]
    }
   ],
   "source": [
    "C = np.vstack((A,B))\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,) (3,) (2, 3)\n"
     ]
    }
   ],
   "source": [
    "print(A.shape,B.shape,C.shape)# 从shape中看出A,B均为拥有3项的数组(数列)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 1 2 2 2]\n"
     ]
    }
   ],
   "source": [
    "# horizontal stack左右合并\n",
    "D = np.hstack((A,B))\n",
    "print(D)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(3,) (3,) (6,)\n"
     ]
    }
   ],
   "source": [
    "print(A.shape,B.shape,D.shape)\n",
    "# (3,) (3,) (6,)\n",
    "# 对于A,B这种，为数组或数列，无法进行转置，需要借助其他函数进行转置"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.2 数组转置为矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 1 1]]\n"
     ]
    }
   ],
   "source": [
    "print(A[np.newaxis,:]) # [1 1 1]变为[[1 1 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(1, 3)\n"
     ]
    }
   ],
   "source": [
    "print(A[np.newaxis,:].shape) # (3,)变为(1, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [1]]\n"
     ]
    }
   ],
   "source": [
    "print(A[:,np.newaxis])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.3 多个矩阵合并"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "------------\n",
      "(3, 1)\n"
     ]
    }
   ],
   "source": [
    "# concatenate的第一个例子\n",
    "print(\"------------\")\n",
    "print(A[:,np.newaxis].shape) # (3,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "A = A[:,np.newaxis] # 数组转为矩阵\n",
    "B = B[:,np.newaxis] # 数组转为矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [1]]\n"
     ]
    }
   ],
   "source": [
    "print(A)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2]\n",
      " [2]\n",
      " [2]]\n"
     ]
    }
   ],
   "source": [
    "print(B)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [1]\n",
      " [1]\n",
      " [2]\n",
      " [2]\n",
      " [2]\n",
      " [2]\n",
      " [2]\n",
      " [2]\n",
      " [1]\n",
      " [1]\n",
      " [1]]\n"
     ]
    }
   ],
   "source": [
    "# axis=0纵向合并\n",
    "C = np.concatenate((A,B,B,A),axis=0)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2]\n",
      " [1 2]\n",
      " [1 2]]\n"
     ]
    }
   ],
   "source": [
    "# axis=1横向合并\n",
    "C = np.concatenate((A,B),axis=1)\n",
    "print(C)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 5.4 合并例子2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "-------------\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "[[0 1 2 3]\n",
      " [4 5 6 7]]\n",
      "-------------\n"
     ]
    }
   ],
   "source": [
    "# concatenate的第二个例子\n",
    "print(\"-------------\")\n",
    "a = np.arange(8).reshape(2,4)\n",
    "b = np.arange(8).reshape(2,4)\n",
    "print(a)\n",
    "print(b)\n",
    "print(\"-------------\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3]\n",
      " [4 5 6 7]\n",
      " [0 1 2 3]\n",
      " [4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "# axis=0多个矩阵纵向合并\n",
    "c = np.concatenate((a,b),axis=0)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 1 2 3 0 1 2 3]\n",
      " [4 5 6 7 4 5 6 7]]\n"
     ]
    }
   ],
   "source": [
    "# axis=1多个矩阵横向合并\n",
    "c = np.concatenate((a,b),axis=1)\n",
    "print(c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6.Numpy array分割"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.1 构造3行4列矩阵"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "A = np.arange(12).reshape((3,4))\n",
    "print(A)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.2 等量分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "# 等量分割\n",
    "# 纵向分割同横向合并的axis\n",
    "print(np.split(A, 2, axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "# 横向分割同纵向合并的axis\n",
    "print(np.split(A,3,axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.3 不等量分割"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2],\n",
      "       [ 6],\n",
      "       [10]]), array([[ 3],\n",
      "       [ 7],\n",
      "       [11]])]\n"
     ]
    }
   ],
   "source": [
    "print(np.array_split(A,3,axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 6.4 其他的分割方式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "# 横向分割\n",
    "print(np.vsplit(A,3)) # 等价于print(np.split(A,3,axis=0))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[array([[0, 1],\n",
      "       [4, 5],\n",
      "       [8, 9]]), array([[ 2,  3],\n",
      "       [ 6,  7],\n",
      "       [10, 11]])]\n"
     ]
    }
   ],
   "source": [
    "# 纵向分割\n",
    "print(np.hsplit(A,2)) # 等价于print(np.split(A,2,axis=1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 7.Numpy copy与 ="
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.1 =赋值方式会带有关联性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "# `=`赋值方式会带有关联性\n",
    "a = np.arange(4)\n",
    "print(a) # [0 1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11  1  2  3]\n"
     ]
    }
   ],
   "source": [
    "b = a\n",
    "c = a\n",
    "d = b\n",
    "a[0] = 11\n",
    "print(a) # [11  1  2  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11  1  2  3]\n"
     ]
    }
   ],
   "source": [
    "print(b) # [11  1  2  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11  1  2  3]\n"
     ]
    }
   ],
   "source": [
    "print(c) # [11  1  2  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11  1  2  3]\n"
     ]
    }
   ],
   "source": [
    "print(d) # [11  1  2  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(b is a) # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(c is a) # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "print(d is a) # True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 22 33  3]\n"
     ]
    }
   ],
   "source": [
    "d[1:3] = [22,33]\n",
    "print(a) # [11 22 33  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 22 33  3]\n"
     ]
    }
   ],
   "source": [
    "print(b) # [11 22 33  3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[11 22 33  3]\n"
     ]
    }
   ],
   "source": [
    "print(c) # [11 22 33  3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 7.2 copy()赋值方式没有关联性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a = np.arange(4)\n",
    "print(a) # [0 1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "b =a.copy() # deep copy\n",
    "print(b) # [0 1 2 3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2 44]\n",
      "[0 1 2 3]\n"
     ]
    }
   ],
   "source": [
    "a[3] = 44\n",
    "print(a) # [ 0  1  2 44]\n",
    "print(b) # [0 1 2 3]\n",
    "\n",
    "# 此时a与b已经没有关联"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 8.广播机制"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "numpy数组间的基础运算是一对一，也就是`a.shape==b.shape`，但是当两者不一样的时候，就会自动触发广播机制，如下例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [10 11 12]\n",
      " [20 21 22]\n",
      " [30 31 32]]\n"
     ]
    }
   ],
   "source": [
    "from numpy import array\n",
    "a = array([[ 0, 0, 0],\n",
    "           [10,10,10],\n",
    "           [20,20,20],\n",
    "           [30,30,30]])\n",
    "b = array([0,1,2])\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "为什么是这个样子？\n",
    "\n",
    "这里以tile模拟上述操作，来回到`a.shape==b.shape`情况！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2]\n",
      " [10 11 12]\n",
      " [20 21 22]\n",
      " [30 31 32]]\n"
     ]
    }
   ],
   "source": [
    "# 对[0,1,2]行重复3次，列重复1次\n",
    "b = np.tile([0,1,2],(4,1))\n",
    "print(a+b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "到这里，我们来给出一张图\n",
    "\n",
    "![](images/3.png)\n",
    "\n",
    "也可以看这张图：\n",
    "![](images/4.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "是不是任何情况都可以呢？\n",
    "\n",
    "当然不是，只有当两个数组的`trailing dimensions  compatible`时才会触发广播，否则报错`ValueError: frames are not aligned exception`。\n",
    "\n",
    "上面表达意思是尾部维度必须兼容！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 9.常用函数"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.1 np.bincount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 2, 1, 2, 1], dtype=int64)"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 3, 0, 1, 4])\n",
    "np.bincount(x)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "统计索引出现次数：索引0出现1次，1出现2次，2出现1次，3出现2次，4出现1次\n",
    "\n",
    "因此通过bincount计算出索引出现次数如下：\n",
    "\n",
    "上面怎么得到的？\n",
    "\n",
    "对于bincount计算吗，bin的数量比x中最大数多1，例如x最大为4，那么bin数量为5(index从0到4)，也就会bincount输出的一维数组为5个数，bincount中的数又代表什么？代表的是它的索引值在x中出现的次数！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还是以上述x为例子，当我们设置weights参数时候，结果又是什么？\n",
    "\n",
    "这里假定："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {},
   "outputs": [],
   "source": [
    "w = np.array([0.3,0.5,0.7,0.6,0.1,-0.9,1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那么设置这个w权重后，结果为多少？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.1, -0.6,  0.5,  1.3,  1. ])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bincount(x,weights=w)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "怎么计算的？\n",
    "\n",
    "先对x与w抽取出来：\n",
    "\n",
    "`x --->  [1, 2, 3, 3, 0, 1, 4]`\n",
    "\n",
    "`w --->  [0.3,0.5,0.7,0.6,0.1,-0.9,1]`\n",
    "索引 0 出现在x中index=4位置，那么在w中访问index=4的位置即可，w[4]=0.1\n",
    "\n",
    "索引 1 出现在x中index=0与index=5位置，那么在w中访问`index=0`与`index=5`的位置即可，然后将两这个加和，计算得：`w[0]+w[5]=-0.6`\n",
    "其余的按照上面的方法即可！\n",
    "\n",
    "bincount的另外一个参数为minlength，这个参数简单，可以这么理解，当所给的bin数量多于实际从x中得到的bin数量后，后面没有访问到的设置为0即可。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "还是上述x为例：\n",
    "\n",
    "这里我们直接设置minlength=7参数，并输出！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0.1, -0.6,  0.5,  1.3,  1. ,  0. ,  0. ])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.bincount(x,weights=w,minlength=7)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "与上面相比多了两个0，这两个怎么会多？\n",
    "\n",
    "上面知道，这个bin数量为5，index从0到4，那么当minlength为7的时候，也就是总长为7，index从0到6，多了后面两位，直接补位为0即可！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.2 np.argmax()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "函数原型为：`numpy.argmax(a, axis=None, out=None)`.\n",
    "\n",
    "函数表示返回沿轴axis最大值的索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "x = [[1,3,3],\n",
    "     [7,5,2]]\n",
    "print(np.argmax(x))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于这个例子我们知道，7最大，索引位置为3(这个索引按照递增顺序)！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "axis属性\n",
    "\n",
    "axis=0表示按列操作，也就是对比当前列，找出最大值的索引！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0]\n"
     ]
    }
   ],
   "source": [
    "x = [[1,3,3],\n",
    "     [7,5,2]]\n",
    "print(np.argmax(x,axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "axis=1表示按行操作，也就是对比当前行，找出最大值的索引！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 1 0]\n"
     ]
    }
   ],
   "source": [
    "x = [[1,3,3],\n",
    "     [7,5,2]]\n",
    "print(np.argmax(x,axis=0))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "那如果碰到重复最大元素？\n",
    "\n",
    "返回第一个最大值索引即可！\n",
    "\n",
    "例如："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 3, 2, 3, 0, 1, 0])\n",
    "print(x.argmax())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.3 上述合并实例"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "这里来融合上述两个函数，举个例子："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n"
     ]
    }
   ],
   "source": [
    "x = np.array([1, 2, 3, 3, 0, 1, 4])\n",
    "print(np.argmax(np.bincount(x)))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "最终结果为1，为什么？\n",
    "\n",
    "首先通过`np.bincount(x)`得到的结果是：`[1 2 1 2 1]`，再根据最后的遇到重复最大值项，则返回第一个最大值的index即可！2的index为1，所以返回1。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 9.4 求取精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1.,  1.,  2., 10., 13.])"
      ]
     },
     "execution_count": 107,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around([-0.6,1.2798,2.357,9.67,13], decimals=0)#取指定位置的精度"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看到没，负数进位取绝对值大的！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.3,  2.4,  9.7, 13. ])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around([1.2798,2.357,9.67,13], decimals=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 1.28,  2.36,  9.67, 13.  ])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around([1.2798,2.357,9.67,13], decimals=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "从上面可以看出，decimals表示指定保留有效数的位数，当超过5就会进位(此时包含5)！\n",
    "\n",
    "但是，如果这个参数设置为负数，又表示什么？"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0,  0,  0, 10, 60])"
      ]
     },
     "execution_count": 110,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around([1,2,5,6,56], decimals=-1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "发现没，当超过5时候(不包含5)，才会进位！-1表示看一位数进位即可，那么如果改为-2呢，那就得看两位！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  0,   0,   0,   0, 100, 200])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.around([1,2,5,50,56,190], decimals=-2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看到没，必须看两位，超过50才会进位，190的话，就看后面两位，后两位90超过50，进位，那么为200！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算沿指定轴第N维的离散差值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  2  3  4  5]\n",
      " [ 6  7  8  9 10]\n",
      " [11 12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "x = np.arange(1 , 16).reshape((3 , 5))\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 113,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1, 1, 1],\n",
       "       [1, 1, 1, 1],\n",
       "       [1, 1, 1, 1]])"
      ]
     },
     "execution_count": 113,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diff(x,axis=1) #默认axis=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 114,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[5, 5, 5, 5, 5],\n",
       "       [5, 5, 5, 5, 5]])"
      ]
     },
     "execution_count": 114,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.diff(x,axis=0) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "取整"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 115,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([-1., -2., -1., -2.,  0.,  1.,  1.])"
      ]
     },
     "execution_count": 115,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.floor([-0.6,-1.4,-0.1,-1.8,0,1.4,1.7])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "看到没，负数取整，跟上述的around一样，是向左！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "取上限"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 2.,  2.,  2.,  3.,  2., -0., -0., -0.])"
      ]
     },
     "execution_count": 116,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.ceil([1.2,1.5,1.8,2.1,2.0,-0.5,-0.6,-0.3])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "取上限！找这个小数的最大整数即可！"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "查找"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用np.where实现小于0的值用0填充吗，大于0的数不变！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  0]\n",
      " [ 2 -2]\n",
      " [-2  1]]\n"
     ]
    }
   ],
   "source": [
    "x = np.array([[1, 0],\n",
    "       [2, -2],\n",
    "     [-2, 1]])\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 118,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [2, 0],\n",
       "       [0, 1]])"
      ]
     },
     "execution_count": 118,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.where(x>0,x,0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
